
#include "sys_font.h"
#include "pt_fs.h"
// ****************************************************************
static  sys_font_info_t  sf_info;

static  void *  sf_font20_buff = NULL;
static  void *  sf_font24_buff = NULL;
static  void *  sf_font60_buff = NULL;
static  void *  sf_font70_buff = NULL;
static  void *  sf_font80_buff = NULL;
static  void *  sf_font90_buff = NULL;
static  void *  sf_font100_buff = NULL;
static  void *  sf_font140_buff = NULL;
// ****************************************************************

int lv_font_create(lv_ft_info_t * font, int height)
{
	int ret = -1;

	font->font      = NULL;
	//font->name      = "/mnt/lfs/MideaType-Regular.ttf";
	font->name      = "./res/MideaType-Regular.ttf";
	font->weight 	= height;
	font->style	    = FT_FONT_STYLE_NORMAL;
	font->mem		= NULL;
	font->mem_size  = 0;
	ret = lv_ft_font_init(font);
	if(!ret) {
		debug_e("create tft failed.\n");
		return -1;
	}
    return 0;
}

int lv_font_get_size(const char* fname)
{	
	int ret =0;
	long int tft_size = 0;
	struct stat fileinfo;
	ret = stat(fname,&fileinfo);
	if(ret == -1){
		debug_e("open err:%s\n",fname);
		return 0;
	}else{
		tft_size = (long int )fileinfo.st_size;
	}

	//debug_d("tft_size:%ld\n",tft_size);

	return tft_size;
}

void * lv_font_bin_create(const char* fname)
{
	int      fd = 0;
	void *   bin_buff = NULL;
	long int bin_size = 0;
	int      read_len = 0;

#ifdef PT_BK7258
	uint32_t start_time = 0;
	uint32_t end_time	= 0;

	start_time = rtos_get_time();
#endif

	bin_size = lv_font_get_size(fname);
	if(!bin_size){
		debug_e("size error\n");
		return NULL;
	}

	bin_buff = psram_malloc(bin_size);
	if(!bin_buff){
		debug_e("psram_malloc error\n");
		return NULL;
	}

	fd = open(fname, O_RDONLY);
	if (fd < 0) {
		debug_e("can't open %s\n", fname);
		return NULL;
	}
	read_len = read(fd,bin_buff,bin_size);
	close(fd);

#ifdef PT_BK7258
	end_time = rtos_get_time();
	debug_d("fst=%s:%d:%d\n",fname,bin_size,end_time - start_time);
#else 
	debug_d("fst=%s\n",fname);
#endif

	return bin_buff;
}

int lv_font_bin_init(void)
{
	sf_font20_buff  = lv_font_bin_create(pt_fs_font_get("midea_font20.bin"));
	sf_font24_buff  = lv_font_bin_create(pt_fs_font_get("midea_font24.bin"));
	sf_font60_buff  = lv_font_bin_create(pt_fs_font_get("midea_font60.bin"));
	sf_font70_buff  = lv_font_bin_create(pt_fs_font_get("midea_font70.bin"));
	sf_font80_buff  = lv_font_bin_create(pt_fs_font_get("midea_font80.bin"));
	sf_font90_buff  = lv_font_bin_create(pt_fs_font_get("midea_font90.bin"));
	sf_font100_buff = lv_font_bin_create(pt_fs_font_get("midea_font100.bin"));
	sf_font140_buff = lv_font_bin_create(pt_fs_font_get("midea_font140.bin"));
	
	return 0;
}

int lv_font24_bin_init(void)
{
	sf_font24_buff  = lv_font_bin_create(pt_fs_font_get("midea_font24.bin"));
	return 0;
}


uint8_t * lv_font_get_buff(int height)
{
	uint8_t * temp = NULL;
	switch(height){
	case 12:
	case 18:
	case 20:
	{
		temp =	(uint8_t *)sf_font20_buff;
		break;
	}	
	case 24:
	case 28:
	{
		temp =	(uint8_t *)sf_font24_buff;
		break;
	}
	case 60:
	{
		temp =	(uint8_t *)sf_font60_buff;
		break;
	}	
	case 70:
	{
		temp =	(uint8_t *)sf_font70_buff;
		break;
	}	
	case 80:
	{
		temp =	(uint8_t *)sf_font80_buff;
		break;
	}	
	case 90:
	{
		temp =	(uint8_t *)sf_font90_buff;
		break;
	}	
	case 100:
	{
		temp =	(uint8_t *)sf_font100_buff;
		break;
	}	
	case 140:
	{
		temp =	(uint8_t *)sf_font140_buff;
		break;
	}
	default:
	{
		temp =	NULL;
		break;
	}
	}

	return temp;
}


lv_font_t * lv_font_get(int height)
{
	lv_font_t * temp = NULL;

#if LV_USE_FREETYPE
	int i   = 0;
	int ret = -1;
	int add_flag = 1;

	for(i=0;i<sf_info.num;i++)
	{
		if(sf_info.font[i].weight == height)
		{
			add_flag = 0;
			break;
		}
	}
	//******************************************************** */
	if(add_flag)
	{
		ret = lv_font_create(&sf_info.font[sf_info.num],height);
		if(ret != 0){
			return CFG_DEFAULT_FONT;
		}
		temp = sf_info.font[sf_info.num].font;
		sf_info.num ++;
	}
	else
	{
		temp = sf_info.font[i].font;
	}
#elif 1
	switch(height){
	case 12:
	case 18:
	case 20:
	{
		if(sf_font20_buff){
			temp = &midea_font20;	
		}else{
			temp = CFG_DEFAULT_FONT;	
		}
		break;
	}	
	case 24:
	case 28:
	{
		if(sf_font24_buff){
			temp = &midea_font24;	
		}else{
			temp = CFG_DEFAULT_FONT;	
		}
		break;
	}
	case 60:
	{
		if(sf_font60_buff){
			temp = &midea_font60;	
		}else{
			temp = CFG_DEFAULT_FONT;	
		}
		break;
	}	
	case 70:
	{
		if(sf_font70_buff){
			temp = &midea_font70;	
		}else{
			temp = CFG_DEFAULT_FONT;	
		}
		break;
	}	
	case 80:
	{
		if(sf_font80_buff){
			temp = &midea_font80;	
		}else{
			temp = CFG_DEFAULT_FONT;	
		}
		break;
	}	
	case 90:
	{
		if(sf_font90_buff){
			temp = &midea_font90;	
		}else{
			temp = CFG_DEFAULT_FONT;	
		}
		break;
	}	
	case 100:
	{
		if(sf_font100_buff){
			temp = &midea_font100;	
		}else{
			temp = CFG_DEFAULT_FONT;	
		}
		break;
	}	
	case 140:
	{
		if(sf_font140_buff){
			temp = &midea_font140;	
		}else{
			temp = CFG_DEFAULT_FONT;	
		}
		break;
	}
	default:
	{
		temp =	CFG_DEFAULT_FONT;
		break;
	}
	}
#elif 1
	temp = CFG_DEFAULT_FONT;
#endif

	return temp;
}
